﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.CompilerServices;
using System.Web.Script.Serialization;
using System.IO;
using System.Web.Hosting;
using bdxyit.com.RecruitmentAppSys.kit;

namespace bdxyit.com.RecruitmentAppSys.Handler
{
    /// <summary>
    /// Summary description for uploadPic
    /// </summary>
    public class uploadPic : IHttpHandler 
    {
        static readonly string  FileRoot = "/photo/";
        private HttpRequest Request { get; set; }
        private HttpResponse Response { get; set; }
        private JavaScriptSerializer Serializer { get; set; }
        public void ProcessRequest(HttpContext context)
        {
            Request = context.Request;
            Response = context.Response;
            Serializer = new JavaScriptSerializer();
            Response.ContentType = "text/html";
            Response.Expires = -1;
            try
            {
                if (Request.ContentLength>102400)
                {
                   throw new Exception("tooBig");
                }else{
                      if (Request.Files.Count <= 0)
                {
                    throw new Exception("nofile");
                }
                else {
                    SaveFile();
                }
                }
            }
            catch (Exception ex)
            {
                var resp = new UploadResponse() { Code = 400, Msg = StringHelper.HtmlSpecialEntitiesEncode(ex.Message) };
                Response.Write(Serializer.Serialize(resp));
            }
            Response.End();
        }

        private void SaveFile()
        {
            var file = Request.Files[0];
            var ext = Path.GetExtension(file.FileName);
            //确保目录存在
            string path = FileRoot + DateTime.Now.ToString("yyyy-MM-dd") + "/";

            if (!Directory.Exists(HostingEnvironment.MapPath(path)))
            {
                Directory.CreateDirectory(HostingEnvironment.MapPath(path));
            }
            //合成文件名
            var filename = path + Guid.NewGuid().ToString("N").Substring(0, 8) + ext;
            var resp = new UploadResponse();
            resp.MIME = file.ContentType;
            resp.Size = file.ContentLength / 1024;
            resp.Name = StringHelper.HtmlSpecialEntitiesEncode(Path.GetFileNameWithoutExtension(file.FileName));
            resp.Path = StringHelper.HtmlSpecialEntitiesEncode(filename);
            resp.Code = 200;
            resp.Msg = "Success";
            //保持文件
            file.SaveAs(System.Web.Hosting.HostingEnvironment.MapPath(filename));
            Response.Write(Serializer.Serialize(resp));
        }



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public class UploadResponse
        {
            public int Code { get; set; }
            public string Msg { get; set; }
            public string Path { get; set; }
            public string Name { get; set; }
            public long Size { get; set; }
            public string MIME { get; set; }
        }
    }
}